home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / IO / File.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.8 KB  |  160 lines

  1.  
  2. package IO::File;
  3.  
  4. =head1 NAME
  5.  
  6. IO::File - supply object methods for filehandles
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.     use IO::File;
  11.  
  12.     $fh = new IO::File;
  13.     if ($fh->open("< file")) {
  14.         print <$fh>;
  15.         $fh->close;
  16.     }
  17.  
  18.     $fh = new IO::File "> file";
  19.     if (defined $fh) {
  20.         print $fh "bar\n";
  21.         $fh->close;
  22.     }
  23.  
  24.     $fh = new IO::File "file", "r";
  25.     if (defined $fh) {
  26.         print <$fh>;
  27.         undef $fh;       # automatically closes the file
  28.     }
  29.  
  30.     $fh = new IO::File "file", O_WRONLY|O_APPEND;
  31.     if (defined $fh) {
  32.         print $fh "corge\n";
  33.  
  34.         $pos = $fh->getpos;
  35.         $fh->setpos($pos);
  36.  
  37.         undef $fh;       # automatically closes the file
  38.     }
  39.  
  40.     autoflush STDOUT 1;
  41.  
  42. =head1 DESCRIPTION
  43.  
  44. C<IO::File> inherits from C<IO::Handle> and C<IO::Seekable>. It extends
  45. these classes with methods that are specific to file handles.
  46.  
  47. =head1 CONSTRUCTOR
  48.  
  49. =over 4
  50.  
  51. =item new ([ ARGS ] )
  52.  
  53. Creates a C<IO::File>.  If it receives any parameters, they are passed to
  54. the method C<open>; if the open fails, the object is destroyed.  Otherwise,
  55. it is returned to the caller.
  56.  
  57. =item new_tmpfile
  58.  
  59. Creates an C<IO::File> opened for read/write on a newly created temporary
  60. file.  On systems where this is possible, the temporary file is anonymous
  61. (i.e. it is unlinked after creation, but held open).  If the temporary
  62. file cannot be created or opened, the C<IO::File> object is destroyed.
  63. Otherwise, it is returned to the caller.
  64.  
  65. =back
  66.  
  67. =head1 METHODS
  68.  
  69. =over 4
  70.  
  71. =item open( FILENAME [,MODE [,PERMS]] )
  72.  
  73. C<open> accepts one, two or three parameters.  With one parameter,
  74. it is just a front end for the built-in C<open> function.  With two
  75. parameters, the first parameter is a filename that may include
  76. whitespace or other special characters, and the second parameter is
  77. the open mode, optionally followed by a file permission value.
  78.  
  79. If C<IO::File::open> receives a Perl mode string ("E<gt>", "+E<lt>", etc.)
  80. or a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic
  81. Perl C<open> operator.
  82.  
  83. If C<IO::File::open> is given a numeric mode, it passes that mode
  84. and the optional permissions value to the Perl C<sysopen> operator.
  85. For convenience, C<IO::File::import> tries to import the O_XXX
  86. constants from the Fcntl module.  If dynamic loading is not available,
  87. this may fail, but the rest of IO::File will still work.
  88.  
  89. =back
  90.  
  91. =head1 SEE ALSO
  92.  
  93. L<perlfunc>, 
  94. L<perlop/"I/O Operators">,
  95. L<IO::Handle>
  96. L<IO::Seekable>
  97.  
  98. =head1 HISTORY
  99.  
  100. Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>.
  101.  
  102. =cut
  103.  
  104. require 5.000;
  105. use strict;
  106. use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD @ISA);
  107. use Carp;
  108. use Symbol;
  109. use SelectSaver;
  110. use IO::Seekable;
  111.  
  112. require Exporter;
  113. require DynaLoader;
  114.  
  115. @ISA = qw(IO::Handle IO::Seekable Exporter DynaLoader);
  116.  
  117. $VERSION = "1.06021";
  118.  
  119. @EXPORT = @IO::Seekable::EXPORT;
  120.  
  121. eval {
  122.     require Fcntl;
  123.     my @O = grep /^O_/, @Fcntl::EXPORT;
  124.     Fcntl->import(@O);  # first we import what we want to export
  125.     push(@EXPORT, @O);
  126. };
  127.  
  128.  
  129.  
  130. sub new {
  131.     my $type = shift;
  132.     my $class = ref($type) || $type || "IO::File";
  133.     @_ >= 0 && @_ <= 3
  134.     or croak "usage: new $class [FILENAME [,MODE [,PERMS]]]";
  135.     my $fh = $class->SUPER::new();
  136.     if (@_) {
  137.     $fh->open(@_)
  138.         or return undef;
  139.     }
  140.     $fh;
  141. }
  142.  
  143.  
  144. sub open {
  145.     @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
  146.     my ($fh, $file) = @_;
  147.     if (@_ > 2) {
  148.     my ($mode, $perms) = @_[2, 3];
  149.     if ($mode =~ /^\d+$/) {
  150.         defined $perms or $perms = 0666;
  151.         return sysopen($fh, $file, $mode, $perms);
  152.     }
  153.     $file = './' . $file if $file =~ m{\A[^\\/\w]};
  154.     $file = IO::Handle::_open_mode_string($mode) . " $file\0";
  155.     }
  156.     open($fh, $file);
  157. }
  158.  
  159. 1;
  160.